package com.hongyu.manager.media.manager.service.impl;

import com.hongyu.manager.media.manager.dao.*;
import com.hongyu.manager.media.manager.model.bean.Video;
import com.hongyu.manager.media.manager.model.bean.VideoType;
import com.hongyu.manager.media.manager.model.bean.Web;
import com.hongyu.manager.media.manager.model.bean.WebSite;
import com.hongyu.manager.media.manager.service.VideoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class VideoServiceImpl implements VideoService {
    private static final Logger LOG = LoggerFactory.getLogger(VideoServiceImpl.class);
    @Autowired
    VideoMapper videoMapper;
    @Autowired
    VideoTypeMapper videoTypeMapper;
    @Autowired
    WebMapper webMapper;


    @Autowired
    WebSiteMapper webSiteMapper;

    @Override
    public List<Video> getList(Video video) {
        List<Video> videos=new ArrayList<>();
        WebSite webSite=webSiteMapper.findByName(video.getWebSite());
        videos = videoMapper.getList(video);

        return videos;
    }

    @Override
    public List<WebSite> webSiteList() {
        return webSiteMapper. webList();
    }

    @Override
    public List<Video> typeList(Video video) {
        WebSite webSite = webSiteMapper.findByName(video.getWebSite());
        List<Video> videos=new ArrayList<>();
        videos = videoMapper.typeList(video);

        return videos;
    }

    @Override
    public void updatePalyTimes(Integer id, String website) {
        WebSite webSite = webSiteMapper.findByName(website);
        Video video  = videoMapper.findVideoById(id);
        video.setPlayTimes(video.getPlayTimes() + 1);
        videoMapper .updatePalyTimes(video);

    }

    @Override
    public List<Web> webList(Web web) {
        return webMapper.webList(web);
    }

    @Override
    public void updateWebSiteTable() {
        List<Video> videos=videoMapper.getAllWebSite();
        for (Video video:videos){
            WebSite website=null;
            LOG.info("WebSite="+video.getWebSite());
            website= webSiteMapper.findByName(video.getWebSite());
            if (website==null){
                website=new WebSite();
                website.setName(video.getWebSite());
                webSiteMapper.addWebSite(website);
            }

        }
    }

    @Override
    public void updateVideoTypeTable() {
        Video bean=null;
        List<Video> videos=videoMapper.getAllWebSite();
        for (Video video:videos){
            bean=new Video();
            bean.setWebSite(video.getWebSite());
            List<Video> videolist = videoMapper.typeList(bean);
            for (Video item:videolist) {
                VideoType videoType=new VideoType();
                videoType.setTypeName(item.getTitle());
                videoType.setWebSite(video.getWebSite());
                List<VideoType> webSiteList=videoTypeMapper.searchTypeList(videoType);
                if (webSiteList.size()<1){
                    videoTypeMapper.addVideoType(videoType);
                }
                LOG.info("web=" + item.getWebSite());
                LOG.info("title=" + item.getTitle());
            }

        }

    }

    @Override
    public void updateVideoType() {
        int pageNum=1;
        int pageSize=100;
        List<Video> videos=new ArrayList<>();

        Video video=new Video();
        video.setStart((pageNum - 1) * pageSize);
        video.setPageSize(pageSize);
        video.setType(0);
        videos=videoMapper.getList(video);
        updateVideoTypeNum(videos);
        while (videos.size()==pageSize){
            pageNum+=1;
            video.setStart((pageNum - 1) * pageSize);
            videos=videoMapper.getList(video);
            updateVideoTypeNum(videos);

        }

    }

    private void updateVideoTypeNum(List<Video> videos) {
        for (Video video:videos){
            VideoType videoType=new VideoType();
            videoType.setWebSite(video.getWebSite());
            videoType.setTypeName(video.getTitle());
            List<VideoType> videoTypeList=videoTypeMapper.searchTypeList(videoType);
            if (videoTypeList.size()>0){
                video.setType(videoTypeList.get(0).getId());
                videoMapper.updateVideoTypeNum(video);
            }
        }
    }
}
